﻿Imports System.IO
Imports System.Text
Imports MeoTom.Core.DAL

Namespace BLL
    Public Class Link
        Public util As New Utilities.Common()

        Private _LinkID As Integer
        Private _LinkName As String
        Private _LinkAddress As String
        Private _LinkDesc As String
        Private _TargetType As Integer
        Private _IsPrivate As Boolean
        Private _LinkCat As Integer

        Public Property LinkCat() As Integer
            Get
                Return _LinkCat
            End Get
            Set(ByVal value As Integer)
                _LinkCat = value
            End Set
        End Property
        Public Property LinkID() As Integer
            Get
                Return _LinkID
            End Get
            Set(ByVal value As Integer)
                _LinkID = value
            End Set
        End Property

        Public Property LinkName() As String
            Get
                Return _LinkName
            End Get
            Set(ByVal value As String)
                _LinkName = value
            End Set
        End Property

        Public Property LinkAddress() As String
            Get
                Return _LinkAddress
            End Get
            Set(ByVal value As String)
                _LinkAddress = value
            End Set
        End Property

        Public Property LinkDesc() As String
            Get
                Return _LinkDesc
            End Get
            Set(ByVal value As String)
                _LinkDesc = value
            End Set
        End Property

        Public Property TargetType() As Integer
            Get
                Return _TargetType
            End Get
            Set(ByVal value As Integer)
                _TargetType = value
            End Set
        End Property

        Public Property IsPrivate() As Boolean
            Get
                Return _IsPrivate
            End Get
            Set(ByVal value As Boolean)
                _IsPrivate = value
            End Set
        End Property

        Public lnkPrv As LinkProvider

        Public Sub New()
            lnkPrv = New LinkProvider()
        End Sub

        Public Sub Create()
            lnkPrv.Create(Me)
        End Sub

        Public Sub Update()
            lnkPrv.Update(Me)
        End Sub

        Public Sub GetByID()
            Try
                Dim reader As SqlClient.SqlDataReader = lnkPrv.GetByID(Me)
                If reader.Read() Then
                    Me.IsPrivate = Boolean.Parse(reader("IsPrivate").ToString())
                    Me.LinkAddress = reader("LinkAddress").ToString()
                    Me.LinkDesc = reader("LinkDesc").ToString()
                    Me.LinkID = Integer.Parse(reader("LinkID").ToString())
                    Me.LinkName = reader("LinkName").ToString()
                    Me.TargetType = Integer.Parse(reader("TargetType").ToString())
                End If
                reader.Close()
            Catch ex As Exception
            End Try
        End Sub

        Public Function GetAllCat(ByVal t As Integer) As String
            Dim strOut As New StringBuilder()
            Try
                Dim reader As SqlClient.SqlDataReader = lnkPrv.GetAllCat(Me)
                If reader.HasRows Then
                    While reader.Read
                        If t = 0 Then
                            'get from inside
                            strOut.Append("<a href=""/LinkManager.aspx?action=getByCat&CatID=").Append(reader("CatID").ToString()).Append(""">").Append(reader("CatName").ToString()).Append("</a>,")
                        ElseIf t = 1 Then
                            'get CatID to edit
                            strOut.Append(reader("CatID").ToString()).Append(",")
                        End If
                    End While
                End If
                reader.Close()
            Catch ex As Exception
            End Try
            Return strOut.ToString()
        End Function

        Public Sub Delete()
            lnkPrv.Delete(Me)
        End Sub

        Public Function GetAll() As DataSet
            Return lnkPrv.GetAll(Me)
        End Function

        Public Function GetByCat(ByVal type As Integer, ByVal LinkCat As Integer) As String
            Dim dbTable As DataTable
            Dim strTargetType As String = ""
            Dim strOut As New StringBuilder()
            Try
                If type = 0 Then
                    Me.LinkCat = LinkCat
                    dbTable = Me.GetAll().Tables(0)
                    For Each row As DataRow In dbTable.Rows
                        Select Case row("TargetType").ToString()
                            Case "0"
                                strTargetType = " target=""_blank"""
                            Case "1"
                                strTargetType = " target=""_top"""
                            Case "2"
                                strTargetType = ""
                            Case Else
                                strTargetType = " target=""_blank"""
                        End Select
                        If row("IsPrivate").ToString() = "False" Then
                            strOut.Append("<li><a href=""").Append(row("LinkAddress").ToString()).Append(""" title=""").Append(row("LinkName").ToString()).Append("""").Append(strTargetType).Append(">").Append(row("LinkName").ToString()).Append("</a></li>")
                        End If
                    Next
                End If
            Catch ex As Exception
            Finally
                dbTable = Nothing
            End Try
            Return strOut.ToString()
        End Function

        Public Sub SetTempLinkCat(ByVal type As Integer)
            lnkPrv.SetTempLinkCat(Me, type)
        End Sub

        Public Sub RemoveTempLinkCat()
            lnkPrv.RemoveTempLinkCat(Me)
        End Sub

        Public Function ReadTopLink() As String
            Dim strOut As String = ""
            Try
                Dim reader As New StreamReader(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Link.htm", System.Text.Encoding.UTF8)
                strOut = reader.ReadToEnd()
                reader.Close()
            Catch ex As Exception
            End Try
            Return strOut
        End Function
    End Class
End Namespace
